import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginPage from "@/views/LoginPage.vue";
import HomePage from "@/views/HomePage.vue";
import MaterialSavePage from "@/views/admin/MaterialSavePage.vue";
import MaterialPage from "@/views/admin/MaterialPage.vue";
import MaterialDetailPage from "@/views/admin/MaterialDetailPage.vue";
import ApiAccessLogPage from "@/views/admin/ApiAccessLogPage.vue";
import MaterialStatisticsPage from "@/views/admin/MaterialStatisticsPage.vue";
import UserPage from "@/views/admin/UserPage.vue";
import PaperManager from "@/views/admin/PaperManager.vue";
import Register from "@/views/Register.vue";
import DeepSeekChat from "@/views/DeepSeekChat.vue";
import PostManage from "@/views/post/PostManage.vue";
import PostList from "@/views/post/PostList.vue";
import RankingList from "@/views/post/RankingList.vue";
import PostLayout from "@/views/post/PostLayout.vue";


Vue.use(VueRouter)

const routes = [
    {path: '/login', component: LoginPage},
    {path: '/user/register', component: Register},
    {
        path: '/', component: HomePage, children: [
            {path: '/material/save', component: MaterialSavePage},
            {path: '/material/detail', component: MaterialDetailPage},
            {path: '/material/statistics', component: MaterialStatisticsPage},
            {path: '/material/page', component: MaterialPage},
            {path: '/user/page', component: UserPage},
            {path: '/api/query', component: ApiAccessLogPage},
            {path: '/paper/page', component: PaperManager},
            {path: '/api/chat', component: DeepSeekChat},
            {
                path: '/posts',
                component:PostLayout,
                children: [
                    {
                        path: 'list',
                        name: 'PostList',
                        component: PostList,
                    },
                    {
                        path: 'manage',
                        name: 'PostManage',
                        component: PostManage,
                    },
                    {
                        path: 'ranking',
                        name: 'RankingList',
                        component:RankingList,
                    }
                ]
            },
        ]
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})


// router是前端的页面路由器对象
// beforeEach方法用于设置在进入每个页面之前执行一个回调
// 这个回调就等同于是前端页面的拦截器
// 这个回调函数有3个参数
// to是要去的页面的路由对象
// from是来自于哪个页面的路由对象
// next是放行函数
router.beforeEach((to, from, next) => {
    // 如果要去的页面是登录页面,直接放行
    if (to.path === '/login'|| to.path === '/user/register') {
        // 定义next()函数就是直接放行
        next()
        // 后面不执行了
        return
    }
    // 如果不上去登录页面, 而是去其他页面
    // 我们在这个回调中发一个请求,问一下后端,这个人的session还活着吗
    // 这里发一个get请求给后端的/session
    Vue.prototype.$http.get('/user/session')
        .then((result) => {
            // 如果后端返回的状态码=10000, 说明通过了后端过滤器的检测,session还活着
            if (result.data.code === 10000) {
                // 放行请求, 让他进入此页面
                next()
            }
            // 如果后端返回的状态码=10007, 说明没有通过后端过滤器的检测,此人未登录或者session已经死亡了
            else if (result.data.code === 10007) {
                // 帮他跳转到登录页面
                next({path: '/login'})
            }
        })
        .catch((error) => {
            console.error('Session check failed:', error);
            next({ path: '/login' }); // 如果请求失败，跳转到登录页面
        })
});
export default router
